/**
 * Author: Ravi Tamada
 * URL: www.androidhive.info
 * twitter: http://twitter.com/ravitamada
 * */
package com.softjourn.punchcard.merchant.lib;

import java.util.HashMap;

import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import com.softjourn.punchcard.merchant.R;
import com.softjourn.punchcard.merchant.lib.PropertiesUtil;
import com.softjourn.punchcard.merchant.model.ApiResponse;
import com.softjourn.punchcard.merchant.model.ClientResponse;
import com.softjourn.punchcard.merchant.model.MerchantResponse;
import com.softjourn.punchcard.merchant.model.OfferResponse;
import com.softjourn.punchcard.merchant.model.ProductsResponse;

import android.content.Context;

public class ApiFunctions {
	
	private static String CLIENT_AUTHORIZATION_URL  = "merchant/{merchantUID}/email/{email}/pass/{pass}/authorization"; 
	private static String PRODUCTS_URL  = "merchant/{merchantUID}/salepoint/{salepointUID}/products"; 
	private static String ADD_BONUSES_URL  = "merchant/{merchantUID}/client/{clientUID}/product/{productId}/addBonuses";
	private static String DEBIT_BONUSES_URL  = "merchant/{merchantUID}/prizeRequest/{punchCardUID}/{offerUID}/debitingBonuses"; 
	private static String CLIENT_INFO_URL = "merchant/{merchantUID}/client/{clientUID}/clientinfo";
	private static String RESET_BONUSES_URL  = "merchant/{merchantUID}/client/{clientUID}/resetBonuses";
	private static String GET_OFFER_URL = "merchant/{offerUID}/offer/{offerUID}/getOffer"; 
	
	private SessionManager session;
		
	private static String PUNCHCARD_API_URL = "http://punch.softjourn.if.ua/punchWS/";
	
	private String merchant_uid;

	
	// constructor
	public ApiFunctions(Context context) {
		
		merchant_uid = PropertiesUtil.getProperty(context, context.getResources().getString(R.string.merchant_property));

		System.setProperty("http.keepAlive", "false");
		
		session = new SessionManager(context); 
		HashMap<String, String> systemDetails = session.getSystemDetails();
		PUNCHCARD_API_URL = systemDetails.containsKey(PUNCHCARD_API_URL) ? systemDetails.get(PUNCHCARD_API_URL) : "http://punch.softjourn.if.ua/punchWS/"; 
		
	}
		
	/**
	 * function return Client and his PuncCard
	 * @param clientUid - Client's Uid
	 * */
	public ClientResponse clientInfo(String clientUid) {
		
		// Create a new RestTemplate instance
		RestTemplate restTemplate = new RestTemplate();

		// Add the Jackson message converter
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

		// Make the HTTP GET request, marshaling the response to a String
		ClientResponse result = restTemplate.getForObject(PUNCHCARD_API_URL+ CLIENT_INFO_URL, ClientResponse.class, merchant_uid, clientUid);
						
		return result;
	}
	
	/**
	 * function make Login Request
	 * @param email
	 * @param password
	 * */
	public MerchantResponse login(String email, String password){

		// Create a new RestTemplate instance
		RestTemplate restTemplate = new RestTemplate();

		// Add the Jackson message converter
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

		// Make the HTTP GET request, marshaling the response to a String
		MerchantResponse result = restTemplate.getForObject(PUNCHCARD_API_URL+ CLIENT_AUTHORIZATION_URL, MerchantResponse.class, merchant_uid, email, password);
					
		return result;

	}

	
	/**
	 * function return merchant's products
	 * @param salePointUid - salePoint's UID
	 * */
	public ProductsResponse getProducts(String salePointUid){
				
		// Create a new RestTemplate instance
		RestTemplate restTemplate = new RestTemplate();

		// Add the Jackson message converter
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

		// Make the HTTP GET request, marshaling the response to a String
		ProductsResponse result = restTemplate.getForObject(PUNCHCARD_API_URL+ PRODUCTS_URL, ProductsResponse.class, merchant_uid, salePointUid);
			
		return result;
	}
	
	
	public ApiResponse addBonuses(String clientUid, String productId) {
		
		// Create a new RestTemplate instance
		RestTemplate restTemplate = new RestTemplate();

		// Add the Jackson message converter
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		
		// Make the HTTP POST request, marshaling the response to a String
		ApiResponse result = restTemplate.postForObject(PUNCHCARD_API_URL+ ADD_BONUSES_URL, null, ApiResponse.class, merchant_uid, clientUid, productId);
		
		return result;	
	}
	
	
	public ApiResponse debitBonuses(String punchCardUid, String offerUid) {
		
		// Create a new RestTemplate instance
		RestTemplate restTemplate = new RestTemplate();

		// Add the Jackson message converter
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		
		// Make the HTTP GET request, marshaling the response to a String
		ApiResponse result = restTemplate.getForObject(PUNCHCARD_API_URL+ DEBIT_BONUSES_URL, ApiResponse.class, merchant_uid, punchCardUid, offerUid);
		
		return result;
	}
	
	
	/**
	 * function reset Client's bonuses
	 * @param uid - client's UID
	 * */
	public ApiResponse resetBonuses(String uid){
		
		// Create a new RestTemplate instance
		RestTemplate restTemplate = new RestTemplate();

		// Add the Jackson message converter
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

		// Make the HTTP GET request, marshaling the response to a String
		ApiResponse result = restTemplate.getForObject(PUNCHCARD_API_URL+ RESET_BONUSES_URL, ApiResponse.class, merchant_uid, uid);
			
		return result;
	}
	
	/**
	 * function return client's punch cards
	 * @param uid - client's UID
	 * */
	public OfferResponse getOffer(String offerUid){
				
		// Create a new RestTemplate instance
		RestTemplate restTemplate = new RestTemplate();

		// Add the Jackson message converter
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

		// Make the HTTP GET request, marshaling the response to a String
		OfferResponse result = restTemplate.getForObject(PUNCHCARD_API_URL+ GET_OFFER_URL, OfferResponse.class, merchant_uid, offerUid);
		
		return result;
	}
	
	/**
	 * Function get Login status
	 * */
	public boolean isUserLoggedIn(Context context){
		if(!session.isLoggedIn()){
			return true;
		}
		return false;
	}
	
	/**
	 * Function to logout user
	 * Reset Database
	 * */
	public boolean logoutUser(Context context){
		session.logoutUser();
		return true;
	}
	
}
